package com.example.binpacking.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.binpacking.mapper.InterviewerMapper;
import com.example.binpacking.mapper.QuestionnaireItemMapper;
import com.example.binpacking.mapper.SysmasterMapper;
import com.example.binpacking.mapper.TeacherMapper;
import com.example.binpacking.model.Interviewer;
import com.example.binpacking.model.QuestionnaireItem;
import com.example.binpacking.model.Sysmaster;
import com.example.binpacking.model.Teacher;
import com.example.binpacking.service.AuthService;
import com.example.binpacking.service.QuestionnaireItemService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class AuthServiceImpl  implements AuthService {
    @Autowired
    private InterviewerMapper interviewerMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SysmasterMapper sysmasterMapper;

    @Override
    public Object login(String loginid, String password, String usertype) {
        //String passwords = DigestUtils.md5Hex(password);

        switch (usertype.toLowerCase()) {
            case "f":
                QueryWrapper<Interviewer> interviewerWrapper = new QueryWrapper<Interviewer>().eq("loginid", loginid).eq("password", password);
                return interviewerMapper.selectOne(interviewerWrapper);
            case "t":
                QueryWrapper<Teacher> teacherWrapper = new QueryWrapper<Teacher>().eq("loginid", loginid).eq("password", password);
                return teacherMapper.selectOne(teacherWrapper);
            case "s":
                //用 QueryWrapper 构建一个查询条件，要求 loginid 列等于传入的 loginid 参数值，并且 password 列等于传入的 password 参数值。
                QueryWrapper<Sysmaster> sysMasterWrapper = new QueryWrapper<Sysmaster>().eq("loginid", loginid).eq("password", password);
                //使用 sysmasterMapper 的 selectOne 方法，根据构建的查询条件从数据库中查询一条符合条件的 Sysmaster 记录
                //查询成功，返回符合条件的 Sysmaster 对象；如果没有找到符合条件的记录，返回 null。
                return sysmasterMapper.selectOne(sysMasterWrapper);
            case "g":
                QueryWrapper<Sysmaster> ordinaryMasterWrapper = new QueryWrapper<Sysmaster>().eq("loginid", loginid).eq("password", password);
                return sysmasterMapper.selectOne(ordinaryMasterWrapper);
            default:
                return null;
        }
    }

    @Override
    public boolean register(String loginid, String password, String usertype) {
//        String encryptedPassword = DigestUtils.md5Hex(password);
        switch (usertype.toLowerCase()) {
            case "f":
                Interviewer interviewer = new Interviewer();
//                interviewer.setLoginid(loginid);
//                interviewer.setPassword(encryptedPassword);
                return interviewerMapper.insert() > 0;
            case "t":
////                Teacher teacher = new Teacher();
////                teacher.setLoginid(loginid);
////                teacher.setPassword(encryptedPassword);
                return teacherMapper.insert() > 0;
            case "s":
//                Sysmaster sysmaster = new Sysmaster();
//                sysmaster.setLoginid(loginid);
////                sysmaster.setPassword(encryptedPassword);
                return sysmasterMapper.insert() > 0;
            case "g":
//                Sysmaster sysmaster = new Sysmaster();
//                sysmaster.setLoginid(loginid);
////                sysmaster.setPassword(encryptedPassword);
                return sysmasterMapper.insert() > 0;
            default:
                return false;
        }
    }
}
